#pragma once
#include "Event.h"
#include <thread>
#include <mutex>
#include <tuple>
namespace xc {
	template<typename ... Args>
	class ThreadEvent :public Event<Args...>
	{
		typedef public Event<Args...> Super;
	private:
		//std::mutex modifyMutex;
		std::mutex callMutex;
		std::list<std::function<void()>> m_callList;
	public:
		virtual xg::Guid onTrigger(EventCallback c) final {
			callMutex.lock();
			auto id = Super::onTrigger(c);
			callMutex.unlock();
			return id;
		}

		virtual void broadcast(Args... args) final {
			callMutex.lock();
			//m_callList.push_back(std::make_tuple(args...));
			auto call = [=]() {
				Super::broadcast(args...);
			};
			m_callList.push_back(call);
			callMutex.unlock();
		}
		void flush() {
			callMutex.lock();
			for (auto call : m_callList) {
				//std::apply(&Super::broadcast,std::make_tuple(1, 2));
				call();
			}
			m_callList.clear();
			callMutex.unlock();
		}
	};
}